package com.kaili.basic.controller;

import com.kaili.basic.pojo.dto.BaseDocumentAddDto;
import com.kaili.basic.pojo.dto.OpinionAddDto;
import com.kaili.basic.pojo.dto.OpinionQueryDto;
import com.kaili.basic.pojo.entity.BaseDocumentEntity;
import com.kaili.basic.pojo.entity.BaseOpinion;
import com.kaili.basic.service.BaseDocumentService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.fastdfs.UploadController;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * 意见建议控制类
 * Created by linjian on 19-7-31.
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "意见建议", description = "意见建议相关API")
@RestController
public class BaseOpinionController {

    @Autowired
    private BaseDocumentService baseDocumentService;

    /**
     * 意见查询
     */
    @ApiOperation(value = "分页查询意见信息", notes = "分页查询意见信息")
    @GetMapping("/opinions")
    public Page<BaseOpinion> getBaseOpinionByCondition(@ModelAttribute OpinionQueryDto opinionQueryDto){
        BaseOpinion baseOpinion = new BaseOpinion();

        return null;
    }

    /**
     * 新增意见
     */
    @ApiOperation(value = "新增意见", notes = "新增意见")
    @PostMapping("/opinions/add")
    @OperateLogAspect(functionName = FunctionName.deviceSign,operateType = OperateType.ADD, key = "imei")
    public RestStatus addOpinions(@ModelAttribute OpinionAddDto dto){
        BaseOpinion opinion = new BaseOpinion();
        BeanUtils.copyProperties(dto,opinion);
        String emails = opinion.getEmails();
        if (null != emails && !emails.equals("")){
            //获得邮箱数组
           String[] email =  emails.split(";");
           if (email.length>10){
               //输入的邮箱超过10个
           }
           //判断邮箱格式是否正确
           boolean flag =  patternMail(email);
           if (!flag){
               return null;
           }
        }
        opinion.setCreatedTime(new Date());

        //增加成功后向抄送的邮箱发送邮件

        return CommonStatus.ADD_OK;
    }

    /**
     * 上传文件
     */
    @ApiOperation(value = "新增文档下发设置")
    @PostMapping("/opinions/upload")
    public RestStatus addDocument(BaseDocumentAddDto dto, @RequestParam("file") MultipartFile file){
        BaseDocumentEntity entity = getFormData(dto,file);
        // 表示服务器下发
        entity.setPattern("1");
        // 如果结束时间不为空，表示是下发设置 ，否则表示设备主动上传
        if(entity.getEndTime() != null){
            entity.setWay("2");
        } else {
            entity.setWay("1");
        }
        baseDocumentService.addDocument(entity);
        return CommonStatus.ADD_OK;
    }

    private BaseDocumentEntity getFormData(BaseDocumentAddDto dto,MultipartFile file) {
        BaseDocumentEntity entity = new BaseDocumentEntity();
        BeanUtils.copyProperties(dto,entity);
        try {
            if (file != null && file.getSize() != 0) {
                String path = UploadController.saveFile(file);
                entity.setFileUrl(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return entity;
    }

    //判断邮箱数组是否正确
    public boolean patternMail(String[] arr){
        //邮箱校验规则
        String martch = "\\w+([-+.]\\w+)*\\.\\w+([-.]\\w+)*";
        for (int i = 0 ; i < arr.length; i++) {
            if (!arr[i].matches(martch)){
                return false;
            }
        }
        return true;
    }

    /**
     * 发送邮件
     */

}
